package cn.stylefeng.guns.modular.cb.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.cb.entity.CbWxUser;
import cn.stylefeng.guns.modular.cb.mapper.CbWxUserMapper;
import cn.stylefeng.guns.modular.cb.model.params.CbWxUserParam;
import cn.stylefeng.guns.modular.cb.model.result.CbWxUserResult;
import  cn.stylefeng.guns.modular.cb.service.CbWxUserService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wj
 * @since 2021-01-20
 */
@Service
public class CbWxUserServiceImpl extends ServiceImpl<CbWxUserMapper, CbWxUser> implements CbWxUserService {

    @Override
    public void add(CbWxUserParam param){
        CbWxUser entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(CbWxUserParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(CbWxUserParam param){
        CbWxUser oldEntity = getOldEntity(param);
        CbWxUser newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public CbWxUserResult findBySpec(CbWxUserParam param){
        return null;
    }

    @Override
    public List<CbWxUserResult> findListBySpec(CbWxUserParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(CbWxUserParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public String findByOpenid(String openid) {
        return this.baseMapper.findByOpenid(openid);
    }

    private Serializable getKey(CbWxUserParam param){
        return param.getOpenid();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private CbWxUser getOldEntity(CbWxUserParam param) {
        return this.getById(getKey(param));
    }

    private CbWxUser getEntity(CbWxUserParam param) {
        CbWxUser entity = new CbWxUser();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    /**
     * 保存/更新 sessionkey
     * @param param
     */
    public void saveWx(CbWxUserParam param){
        String openid = this.findByOpenid(param.getOpenid());
        if(StringUtils.isEmpty(openid)){
            this.add(param);
        }else {
            this.update(param);
        }
    }

    @Override
    public List<Map<String, Object>> queryDateCount(String startDate, String[] countArr) {
        return this.baseMapper.queryDateCount(startDate, countArr);
    }

    @Override
    public List<Map<String, Object>> queryMouthCount(String startDate, String[] countArr) {
        return this.baseMapper.queryMouthCount(startDate, countArr);
    }
}
